home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2934 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.1 KB

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Virtual Members: Difficult Question
  5. Date: Sat, 20 Jan 1996 19:07:34 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4dregt$i22@news.halcyon.com>
  8. References: <4dmha6$80c@marlin.ssnet.com>
  9. NNTP-Posting-Host: blv-pm3-ip2.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. helie@ssnet.com (Ray Helie) wrote:
  13.  
  14. >I have a difficult problem ...
  15. >class C_BASE : public CObject
  16. >    {
  17. >    // ...
  18. >    virtual void index () = 0;
  19. >    };
  20.  
  21.  
  22. >I don't really use any of the functions out of CObject, but I have to
  23. >inherit it to pass C_BASE to other objects....
  24.  
  25. >class C_ITEM : public C_BASE
  26. >    {
  27. >    // ...
  28. >    void index () { ... };
  29. >    };
  30.  
  31.  
  32. >...(and this part is a prelude to the tough
  33. >part):
  34.  
  35. >void main ()
  36. >    {
  37. >    C_ITEM item;
  38. >    func1 ((CObject*)&item);
  39. >    }
  40.  
  41. >void func1 (CObject* pItem)
  42. >    {
  43. >    C_BASE* base;
  44. >    base = pItem;
  45. >    pItem-> index (); 
  46. >    // (i) is the above call to index () allowed?  or did i lose virtual 
  47. >    // information about the class C_BASE when I went to the CObject
  48. >    // pointer?
  49. >    }
  50.  
  51.  
  52. >That seems to work for me, but I thought I'd make sure it's something
  53. >that is truly legal that is supposed to work *all* the time.  
  54.  
  55. BTW, If func1 is calling pItem->index(), then func1 really *does* take
  56. C_BASE * or better, not CObject *.  I'd reconsider the argument type
  57. to func1 first!
  58.  
  59. Generally down-casts are unsafe.  Is this VC4.0?  You could try to
  60. dynamic_cast<C_BASE*>(pItem).  Compile with RTTI turned on (/GR), then
  61. if pItem doesn't really point to a C_BASE (or derivative thereof),
  62. dynamic_cast will return NULL.  This is part of a recent draft of C++
  63. to standardize run-time type info.  Any set of classes with virtual
  64. functions will be operable this way.  
  65.  
  66. >On to the difficult part:
  67. >I'm reading and writing object states to disk.  I've have need to do it
  68. >the following way, so what I need to know is why the following way
  69. >doesn't seem to work and how I can get around it:
  70.  
  71.  
  72. >main ()
  73. >    {
  74. >    C_ITEM item;
  75. >    writeObject ((CObject*)&item,sizeof(C_ITEM));
  76. >    readObject  ((CObject*)&item,sizeof(C_ITEM));
  77. >    }
  78.  
  79.  
  80. >void readObject (CObject* pObject,int pSize)
  81. >    {
  82. >    C_BASE* base = pObject;
  83. >    base-> index (); // this call works fine
  84. >    Read ((char*)&pObject,pSize); 
  85. >    // this reads in the character string from beginning of a file
  86.  
  87. >    base-> index (); // !!! this call now crashes the program !!!
  88.  
  89. >    }
  90.  
  91.  
  92. >void writeObject (CObject* pObject,int pSize)
  93. >    {
  94. >    Write ((char*)pObject,pSize);
  95. >    // this will write a string of characters to the start of a file
  96. >    // ...
  97. >    }
  98.  
  99.  
  100. >So why does the line in readObject () crash...
  101. >Ray Helie [helie@ssnet.com]
  102.  
  103. It would be preferrable, of course, to just override CObject's
  104. Serialize() method and persist data member by member, each derivation
  105. remembering to call it's immediate base class' Serialize(), etc.  
  106.  
  107. From the sounds of it, the blanket read of sizeof(C_ITEM) 
  108. bytes for pObject corrupts the vtable of the C_BASE reference.
  109. Perhaps the debugger could shed some light.  Anyway, dynamic_cast
  110. *might* solve the problem if you're dead-set against using the
  111. customary Serialize() approach.  
  112.  
  113. Hope this helps.
  114.                     --Norm
  115.  
  116.